package org.sixtynine.stock.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.PaymentConfig;
import org.sixtynine.stock.entity.User;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.PaymentConfigService;
import org.sixtynine.stock.service.content.DataManagerService;
import org.sixtynine.stock.service.file.FileDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 *         this class use to add , edit and delete payment config data
 * 
 */

@Controller
@SessionAttributes
public class IpgConfigController extends BackEndAuthController {

	@Autowired
	private GenericService genericService;

	@Autowired
	private FileDataService fileDataService;

	@Autowired
	DataManagerService dataManagerService;

	@Autowired
	PaymentConfigService paymentConfigService;

	@RequestMapping(value = "/admin/payment_config/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editPaymentConfig(HttpServletRequest request,
			@PathVariable Integer id) {
		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}
		modelAndView = new ModelAndView("/admin/payment_config/edit");
		BaseEntity paymentConfig = genericService.findById(id,
				PaymentConfig.class);
		modelAndView.addObject("paymentConfig", paymentConfig);
		modelAndView.addObject("userLogged", true);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/payment_config/edit/{id}", method = RequestMethod.POST)
	public ModelAndView edditingPaymentConfig(
			@ModelAttribute @Valid PaymentConfig paymentConfig,
			BindingResult result, @PathVariable Integer id) {
		ModelAndView modelAndView = null;
		if (!result.hasErrors()) {
			genericService.saveOrUpdate(paymentConfig);
			modelAndView = new ModelAndView("/admin/payment_config/edit");
			List<BaseEntity> paymentConfigs = genericService
					.findAll(PaymentConfig.class);
			modelAndView.addObject("paymentConfigs", paymentConfigs);

			String message = "Payment Config was successfully edited.";
			modelAndView.addObject("message", message);

			return modelAndView;
		} else {
			modelAndView = new ModelAndView("/admin/payment_config/edit");
			BaseEntity paymentConfigData = genericService.findById(id,
					PaymentConfig.class);
			modelAndView.addObject("paymentConfig", paymentConfigData);
			modelAndView.addObject("userLogged", true);

			return modelAndView;
		}
	}
}
